/*
 * GeneralParameterSet.java
 *
 */

package gotha;

/**
 *
 * @author Luc Vannier
 */
public class GeneralParameterSet implements java.io.Serializable{
    private static final long serialVersionUID = Gotha.GOTHA_DATA_VERSION;
   
    final static int GEN_MM_FLOOR_MIN   = -30;  // 30K
    final static int GEN_MM_FLOOR_MAX   =  0;   // 1D
    final static int GEN_MM_BAR_MIN     = -10;  // 10K
    final static int GEN_MM_BAR_MAX     =  8;   // 9D

    
    /**  For instance : paris2009
         shortName is the default file name*/
    private String shortName;
    /**  For instance : Toyota Tour Paris Tournament 2009 */
    private String name;
    private String location;
    private java.util.Date beginDate;
    private java.util.Date endDate;
    
    // Games parameters
    private String strTime = "";
    private boolean bByoYomi = true;
    private String strSize = "";
    private String strKomi = "7.5";
    
    private int numberOfRounds;  
    private int numberOfCategories;
    private int[] lowerCategoryLimits; // limits are defined in rank units (between +8 and -30)
    private int genMMFloor;
    private int genMMBar;

    private int genNBW2ValueAbsent    = 0;  // 2 * Number of NBW points for a player absent of a round
    private int genNBW2ValueBye = 1;        // 2 * Number of NBW points for a player not paired in a round (uneven)

    private int genMMS2ValueAbsent = 1;     // 2 * Number of MMS points for a player absent of a round
    private int genMMS2ValueBye = 1;        // 2 * Number of MMS points for a player not paired in a round   
    
    /** Creates a new instance of GeneralParameterSet */
    public GeneralParameterSet(String shortName, String name, String location, 
            java.util.Date beginDate, java.util.Date endDate, int numberOfRounds, int numberOfCategories) {
        init(shortName, name, location, 
            beginDate, endDate, numberOfRounds,  numberOfCategories); 
    }

    public GeneralParameterSet() {
        init("Undefined", "Undefined", "", new java.util.Date (),  new java.util.Date (), 5, 1);
    }
    public GeneralParameterSet(GeneralParameterSet gps) {
        init(gps.getShortName(),
                gps.getName(),
                gps.getLocation(),
                gps.getBeginDate(),
                gps.getEndDate(),
                gps.getNumberOfRounds(),
                gps.getNumberOfCategories());
        int[] llc = gps.getLowerCategoryLimits();
        int[] llc2 = null;
        if (llc !=null){
            llc2 = new int[llc.length];
            for (int c = 0; c < llc.length; c++)
                llc2[c] = llc[c];
            this.setLowerCategoryLimits(llc2);
        }
    }
    
    public void initForMM(){
        setNumberOfCategories(1);
        genMMFloor            = -20;      // 20K
        genMMBar              = 3;        // 4D    
        
        genNBW2ValueAbsent      = 0;        
        genNBW2ValueBye         = 1;       

        genMMS2ValueAbsent      = 1;       
        genMMS2ValueBye         = 1;        
    }
    
    public void initForSwiss(){
        setNumberOfCategories(1);
        genMMFloor            = -30;    // Not relevant
        genMMBar              = 8;      // Not relevant

        genNBW2ValueAbsent      = 0;    // 2 * Number of NBW points for a player absent of a round
        genNBW2ValueBye         = 1;    // 2 * Number of NBW points for a player not paired in a round (uneven)

        genMMS2ValueAbsent      = 1;    // Not relevant
        genMMS2ValueBye         = 1;    // Not relevant     
        
    }
    
    public void initForSwissCat(){
        setNumberOfCategories(3);
        lowerCategoryLimits[0] = 0;
        lowerCategoryLimits[1] = -5;
        genMMFloor            = -30;    // Relevant only for pseudo MMS in sec criteria pairing
        genMMBar              = 8;  // Relevant only for pseudo MMS in sec criteria pairing
                
        genNBW2ValueAbsent      = 0;    // 2 * Number of NBW points for a player absent of a round
        genNBW2ValueBye         = 1;    // 2 * Number of NBW points for a player not paired in a round (uneven)

        genMMS2ValueAbsent      = 1;    // Not relevant
        genMMS2ValueBye         = 1;    // Not relevant     
        

    }
    
    public void init(String shortName, String name, String location, 
            java.util.Date beginDate, java.util.Date endDate, int numberOfRounds,  int numberOfCategories) {
        setShortName(shortName);
        this.name = name;
        this.location = location;
        this.beginDate = beginDate;
        this.endDate = endDate;
        this.numberOfRounds = numberOfRounds;
        setNumberOfCategories(numberOfCategories);
    }
    
    public String getShortName() {
        return shortName;
    }

    public void setShortName(String shortName) {
        // Eliminate all characters after "."
        int pos = shortName.indexOf(".");
        if (pos >= 0) shortName = shortName.substring(0, pos);
        if(shortName.length() < 1) shortName = "TournamentShortName";
        this.shortName = shortName;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }

    public java.util.Date getBeginDate() {
        return beginDate;
    }

    public void setBeginDate(java.util.Date beginDate) {
        this.beginDate = beginDate;
    }

    public java.util.Date getEndDate() {
        return endDate;
    }

    public void setEndDate(java.util.Date endDate) {
        this.endDate = endDate;
    }

    public int getNumberOfRounds() {
        return numberOfRounds;
    }

    public void setNumberOfRounds(int numberOfRounds) {
        this.numberOfRounds = numberOfRounds;
    }

    public int getNumberOfCategories() {
        return numberOfCategories;
    }

    public void setNumberOfCategories(int numberOfCategories) {
        this.numberOfCategories = numberOfCategories;

        if (numberOfCategories <= 0){
            System.out.println("Internal issue. Number of categories < = 0");
            numberOfCategories = 1;
        }

        // Ensure coherence between numberOfCategories and lowerCategoryLimits
        if (numberOfCategories == 1){
            lowerCategoryLimits = null;
            return;
        }

        if (this.lowerCategoryLimits == null) lowerCategoryLimits = new int[numberOfCategories - 1];

        if ( (numberOfCategories - 1) < lowerCategoryLimits.length){
            int[] lcl = new int[numberOfCategories - 1];
            for (int c = 0; c < numberOfCategories - 1; c++){
                lcl[c] = lowerCategoryLimits[c];
            }
            setLowerCategoryLimits(lcl);
            return;
        }
        if ( (numberOfCategories - 1) > lowerCategoryLimits.length){
            int[] lcl = new int[numberOfCategories - 1];
            for (int c = 0; c < lowerCategoryLimits.length; c++){
                lcl[c] = lowerCategoryLimits[c];
            }
            for (int c = lowerCategoryLimits.length; c < numberOfCategories - 1; c++){
                lcl[c] = Gotha.MIN_RANK;
            }
            setLowerCategoryLimits(lcl);
            return;
        }
    }

    public int[] getLowerCategoryLimits() {
        return lowerCategoryLimits;
    }

    public void setLowerCategoryLimits(int[] lowerCategoryLimits) {
        this.lowerCategoryLimits = lowerCategoryLimits;
    }

    public int getGenMMBar() {
        return genMMBar;
    }

    public void setGenMMBar(int genMMBar) {
        this.genMMBar = genMMBar;
    }

    public int getGenMMFloor() {
        return genMMFloor;
    }

    public void setGenMMFloor(int genMMFloor) {
        this.genMMFloor = genMMFloor;
    }

    public int getGenNBW2ValueAbsent() {
        return genNBW2ValueAbsent;
    }

    public void setGenNBW2ValueAbsent(int genNBW2ValueAbsent) {
        this.genNBW2ValueAbsent = genNBW2ValueAbsent;
    }

    public int getGenNBW2ValueBye() {
        return genNBW2ValueBye;
    }

    public void setGenNBW2ValueBye(int genNBW2ValueBye) {
        this.genNBW2ValueBye = genNBW2ValueBye;
    }

    public int getGenMMS2ValueAbsent() {
        return genMMS2ValueAbsent;
    }

    public void setGenMMS2ValueAbsent(int genMMS2ValueAbsent) {
        this.genMMS2ValueAbsent = genMMS2ValueAbsent;
    }

    public int getGenMMS2ValueBye() {
        return genMMS2ValueBye;
    }

    public void setGenMMS2ValueBye(int genMMS2ValueBye) {
        this.genMMS2ValueBye = genMMS2ValueBye;
    }

    public String getStrTime() {
        return strTime;
    }

    public void setStrTime(String strTime) {
        this.strTime = strTime;
    }

    public String getStrSize() {
        return strSize;
    }

    public void setStrSize(String strSize) {
        this.strSize = strSize;
    }

    public String getStrKomi() {
        return strKomi;
    }

    public void setStrKomi(String strKomi) {
        this.strKomi = strKomi;
    }

    public boolean isBByoYomi() {
        return bByoYomi;
    }

    public void setBByoYomi(boolean bByoYomi) {
        this.bByoYomi = bByoYomi;
    }
    
}
